﻿using Finbook.Entity;
using Finbook.Entity.Cmds;
using GreenPipes;
using MassTransit;
using Nest;
using StackExchange.Redis;
using System;
using System.Threading.Tasks;

namespace Finbook.ConsoleView
{
    class Program
    {
        private static string RedisConnectionString = "116.196.97.224:6379";
        private static string ProjectName = "PPM.VerticalSite";
        private static string DomainName = "WaresOnlineDomain";
        private static string KeyPrefix = $"{ProjectName}:{DomainName}";
        static void Main(string[] args)
        {
            //ConsumerTestAsync().Wait();
            MassTransitCmdTestAsync().Wait();
            //ElasticSearchTest();
        }

        private static void ElasticSearchTest()
        {
            var client = new ElasticClient(new ConnectionSettings(new Uri("")));
        }

        private static async Task MassTransitCmdTestAsync()
        {
            var bus = Bus.Factory.CreateUsingRabbitMq(cfg =>
            {
                var host = cfg.Host(new Uri(ConfigSettings.ServerUrl), hostCfg =>
                {
                    hostCfg.Username(ConfigSettings.UserName);
                    hostCfg.Password(ConfigSettings.Password);
                });
                cfg.UseRetry(retryCfg =>
                {
                    retryCfg.Interval(3, 10);
                });
                cfg.UseRateLimit(1000, TimeSpan.FromSeconds(100));
                cfg.UseCircuitBreaker(circuitBreakCfg =>
                {
                    circuitBreakCfg.TrackingPeriod = TimeSpan.FromMinutes(5);
                    circuitBreakCfg.TripThreshold = 15;
                    circuitBreakCfg.ActiveThreshold = 10;
                    circuitBreakCfg.ResetInterval = TimeSpan.FromMinutes(1);
                });
            });
            bus.Start();
            var i = 0;
            //var endPoint = await bus.GetSendEndpoint(new Uri($"{ConfigSettings.ServerUrl}cmdQueue"));
            //var client = bus.CreateRequestClient<TestRequest,TestResponse>(new Uri($"{ConfigSettings.ServerUrl}requestQueue"),TimeSpan.FromMinutes(1));
            while (true)
            {
                Console.WriteLine("请输入您要发送的命令：");
                var input = Console.ReadLine();
                if (!string.IsNullOrEmpty(input))
                {
                    await bus.Publish(new TestPublishedEvent() { Id = i++, Name = input });
                    //await endPoint.Send(new TestPublishedEvent() { Id = i++, Name = $"发送消息{input}" });
                    //await client.Request(new TestRequest() { Id = 1, Name = $"请求消息{input}" });
                }
            }
        }

        private static async Task ConsumerTestAsync()
        {
            using (var myConn = await ConnectionMultiplexer.ConnectAsync(RedisConnectionString))
            {
                var db = myConn.GetDatabase();
                var key = $"{KeyPrefix}:EmailQueue";
                while (true)
                {
                    var output = await db.ListRightPopAsync(key);
                    if (!string.IsNullOrEmpty(output))
                    {
                        Console.WriteLine($"当前接受到的邮件任务：{output}");
                    }
                }
                //var vals = await db.ListRangeAsync(key, 0, -1);
                //var val = await db.ListLeftPopAsync(key);
                //val = await db.ListRightPopAsync(key);
            }
        }
    }
}
